/**
 * Created by hjqi on 2016/5/9.
 */

var UserAccountInfoDB = require('../../db/userAccountInfoDB');
var _userAccountInfodb = new UserAccountInfoDB();
var _deepCopy = require('../common/common').deepCopy;

var config = require('../config/config').config;

module.exports = libUserAccount;

function libUserAccount(){

}

libUserAccount.prototype.createAccountInfo = function(username){
    return _createAccountInfo(username);
}
/**
 * update the resource quotas  data
 * @deprecated
 */
libUserAccount.prototype.requestResourceUpdate = function(username,limit){

    return new Promise(function(resolve, reject) {
        var _ret = {};
        getAccountInfo(username,limit).then(function (ret) {
            if(ret.length > 0){
                var _core = ret[0].cores - limit.cores;
                var _mem = ret[0].memory - limit.memory;
                return updateAccountInfo(username,{
                    cores:_core,
                    memory:_mem,
                    username:username
                });
            }else
                reject("not valid account");
        }).then(function(){
            resolve(true);
        }).catch(function (err) {
            reject(err);
        });
    });
}

/*libUserAccount.prototype.releaseResourceUpdate = function(username,limit){
    return new Promise(function(resolve, reject) {
        var _ret = {};
        getAccountInfo(username,limit).then(function (ret) {
            if(ret.length > 0){
                var _core = ret[0].cores + limit.cores;
                var _mem = ret[0].memory + limit.memory;
                return updateAccountInfo(username,{
                    cores:_core,
                    memory:_mem,
                    username:username
                });
            }else
                reject("not valid account");
        }).then(function(){
            resolve(true);
        }).catch(function (err) {
            reject(err);
        });
    });
}*/
/**
 * check the resource quotas is enough
 * @deprecated since 16/9/26
 * @returns {Promise}
 */
libUserAccount.prototype.checkAccountBalance  = function(username,limit){

    return new Promise(function(resolve, reject){
        var _ret = {};
        getAccountInfo(username,limit).then(function(ret){
            if(ret.length > 0){
                if( ret[0].cores >= limit.cores && ret[0].memory >= limit.memory){
                    resolve(true);
                }else{
                    _ret.message = "no enough cores or memory";
                    reject(_ret);
                }
            }else{
                _createAccountInfo(username).then(function(){
                    resolve(true);
                }).catch(function(err){
                    /*_ret.message = "not valid account";
                     reject(_ret);*/
                    reject(err);
                });

                /*_ret.message = "not valid account";
                reject(_ret);*/
            }
        }).catch(function(err){
            reject(err);
        });
    });

}

/**
 * calculate the rest resources
 * "limits":{
    "cores":0.5,
    "memory":1024
  }
 */
libUserAccount.prototype.calcRest  = function(username,occupied) {
    var mem = 0 ;
    var cores= 0 ;
    var _limits ={} ;

    return new Promise(function(resolve, reject){
        var _ret = {};
        getAccountInfo(username,{}).then(function(ret){
            if(ret.length > 0){
                _limits =  ret[0] ;
                if(occupied){
                    mem = _limits.memory - occupied.memory ;
                    cores = _limits.cores - occupied.cores ;
                    resolve({
                        "cores": cores,
                        "memory": mem
                    });
                }else{
                    _ret.message = "no enough cores or memory";
                    reject(_ret);
                }
            }else{
                _ret.message = "["+username+"] getAccountInfo error";
                reject(_ret);
            }
        }).catch(function(err){
            var _errmsg = "calculation exception:"+err.message;
            logger.error(_errmsg);
            reject(err);
        });
    });
}

function getAccountInfo(username,limit){
    return new Promise(function(_resolve, _reject){

        limit.username = username;

        _userAccountInfodb.getWithParam(null,{username:username},function(err,rets){
            if(err){
                _reject(err);
            }else{
                _resolve(rets);
            }
        });
    });
}

function updateAccountInfo(username,limit){
    return new Promise(function(_resolve, _reject){

        limit.username = username;

        _userAccountInfodb.updateWithParam(null,{username:username},limit,function(err){
            if(err){
                _reject(err);
            }else{
                _resolve(true);
            }
        });
    });
}

function _createAccountInfo(username){
    return new Promise(function(_resolve, _reject){

        var _limits = _deepCopy(config.limits);
        _limits.username = username;

        _userAccountInfodb.updateOrInsert(null,_limits,function(err){
            if(err){
                _reject(err);
            }else{
                _resolve(true);
            }
        });
    });
}